﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace LevelEditor.Classes
{
    public class TextHandler
    {
        private LevelEditor editor;
        private String test;

        public TextHandler(LevelEditor editor)
        {
            this.editor = editor;
        }
        public void load()
        {
            StreamReader tr;
            // create reader & open file
            tr = new StreamReader("Classes/Objects/objectlist.txt");
            // read a line of text
            String content;
            while ((content = tr.ReadLine()) != null)
            {
                String[] lineContent = content.Split(':');
                String name = lineContent[0];
                String path = (Application.StartupPath + "\\Classes\\" + lineContent[1]);
                Image image = new Bitmap(path);
                String type = lineContent[2];
                CustomObject cuObject = new CustomObject(image, "Kinematic", new Microsoft.Xna.Framework.Vector2(0, 0), 0f, name, 0,
                    new Microsoft.Xna.Framework.Vector2(image.Width, image.Height), path);
                cuObject.relativeImageUrl = lineContent[1];
                switch (type)
                {
                    case "Normal": cuObject.currentType = CustomObject.OType.Normal;
                        editor.normalBox.Items.Add(cuObject.name);
                        break;
                    case "Ground": cuObject.currentType = CustomObject.OType.Ground;
                        editor.groundBox.Items.Add(cuObject.name);
                        break;
                    case "Item": cuObject.currentType = CustomObject.OType.Item;
                        editor.itemBox.Items.Add(cuObject.name);
                        break;
                }

                editor.objects.Add(cuObject);

            }
            // close the stream
            tr.Close();
        }
        public void save()
        {
            // create a writer and open the file
            using (TextWriter tw = File.CreateText("Classes/Objects/objectlist.txt"))
            {
                // write a line of text to the file
                foreach (CustomObject cu in editor.objects)
                {
                    tw.WriteLine(cu.name + ":" + cu.relativeImageUrl + ":" + cu.currentType);
                }
                //close the stream
                //tw.Close();
            }
        }
    }
}
